﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using db.Model;
using Newtonsoft.Json;

namespace db.view.New
{
    public class MatchInfo:rui.pagerBase
    {
        public int ID { get; set; }

        [Display(Name = "用户编码")]
        public string Name { get; set; }

        [Display(Name = "开始时间")]
        public DateTime? StartTime { get; set; }

        [Display(Name = "结束时间")]
        public DateTime? EndTime { get; set; }

        [Display(Name = "比赛简介")]
        public string Info { get; set; }

        [Display(Name = "比赛地点")]
        public string Place { get; set; }

        [Display(Name = "裁判")]
        public string Judge { get; set; }


        public override void Search()
        {
            this.keyField = "ID";
            this.ResourceCode = "matchInfo";
            string querySql = @" SELECT * FROM Match WHERE 1=1 ";
            
            this.getPageConfig();
            rui.pagerHelper ph = new rui.pagerHelper(querySql, this.getOrderSql("ID", "desc"), this);
            ph.Execute(this.PageSize, this.PageIndex, this);
            this.dataTable = ph.Result;

            //显示的列和列顺序
            this.showColumn = this.getShowColumn();
        }

        public static MatchInfo getEntryByRowID(string rowId, dbEntities dc)
        {
            var model = dc.Match.FirstOrDefault(r => r.ID.ToString() == rowId);
            var json = JsonConvert.SerializeObject(model);
            return JsonConvert.DeserializeObject<MatchInfo>(json);
        }

        public static string createRelatedUser(MatchInfo model, dbEntities dc)
        {
            var json = JsonConvert.SerializeObject(model);
            dc.Match.Add(JsonConvert.DeserializeObject<Match>(json));
            dc.SaveChanges();
            return "OK";
        }
    }
}
